package com.ihg.hiex.admin.processor.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.ihg.hiex.admin.processor.FtpProcessor;
import com.ihg.hiex.admin.rao.FtpRao;

/**
 *
 * @author Ark
 *
 */
@Component("ftpProcessor")
public class FtpProcessorImpl implements FtpProcessor {

    private static final Logger logger = LoggerFactory.getLogger(FtpProcessorImpl.class);

    @Value("#{config['ftp.host']}")
    private String ftpHost;
    
    @Rao
    private FtpRao ftpRao;

    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.processor.FtpProcessor#createDirectory(java.lang.String, java.lang.String)
     */
    public void createDirectory(List<String> sessionNames, List<String> delegateNames) {
        
        // 1. initialize create directory bash
        Map<String, Object> directoryInfos = new HashMap<String, Object>();
        directoryInfos.put("session", sessionNames);
        directoryInfos.put("delegates", delegateNames);
        
        String createBashUrl = ftpHost + "/make_folders";
        HttpResult httpResult = ftpRao.createBash(createBashUrl, directoryInfos);
        if (!httpResult.isValid()) {
            logger.error("#createDirectory() - initialize create directory bash fail, exception=[{}]", httpResult.getMessage());
            throw BusinessException.newInstance("500", "创建目录脚本获取失败");
        }
        
        // 2. execute create directory bash
        String executeBashUrl = ftpHost + "/execute_bash";
        httpResult = ftpRao.executeBash(executeBashUrl, Collections.emptyMap());
        if (!httpResult.isValid()) {
            logger.error("#createDirectory() - execute create directory bash fail, exception=[{}]", httpResult.getMessage());
            throw BusinessException.newInstance("500", "执行创建目录脚本失败");
        }
        
    }
    
}
